package org.come.tt;

import com.gl.util.Xy2Util;
import com.tool.btn.FormsOnOffBtn;
import com.tool.image.ImageMixDeal;
import com.tool.role.RoleData;
import com.tool.tcp.NewPart;
import com.tool.tcp.PartOne;
import com.tool.tcp.PartTwo;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;

import org.come.bean.ConfigureBean;
import org.come.bean.LoginResult;
import org.come.model.Configure;
import org.come.until.CutButtonImage;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class LadderJpanel extends JPanel {

    // 背景图片
    private ImageIcon beijing;

    // 头像图片
    private JLabel headImg;

    private LadderPanelBtn startTheGame, ladderPoints, ladderLottery, award1, award2, award3;

    private String seasonInfo = "";
    private String currSeason = "";

    private List<LoginResult> loginResultList;

    public String getSeasonInfo() {
        return seasonInfo;
    }

    public void setSeasonInfo(String seasonInfo) {
        this.seasonInfo = seasonInfo;
    }

    public String getCurrSeason() {
        return currSeason;
    }

    public void setCurrSeason(String currSeason) {
        this.currSeason = currSeason;
    }

    public List<LoginResult> getLoginResultList() {
        return loginResultList;
    }

    public void setLoginResultList(List<LoginResult> loginResultList) {
        this.loginResultList = loginResultList;
    }

    public LadderJpanel() {
        ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
        Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
        Configure configure = item.get(new BigDecimal(1));
        String nao = "新";
        if (configure.getNeworold() != null) {
            nao = configure.getNeworold();
        }
        this.setPreferredSize(new Dimension(682, 475));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);

        beijing = new ImageIcon("inkImg/background/S284.png");

        headImg = new JLabel();
        headImg.setBounds(30, 50, 75, 100);
        if (nao.equals("新")) {
            ImageIcon Img = CutButtonImage.getImage("img/head/" + RoleData.getRoleData().getLoginResult().getSpecies_id() + ".png", 75, 100);
            headImg.setIcon(Img);
            this.add(headImg);
        } else {
            ImageIcon Img = CutButtonImage.getImage("img/head/" + RoleData.getRoleData().getLoginResult().getSpecies_id() + ".png", 75, 100);
            headImg.setIcon(Img);
            this.add(headImg);
        }

        //开始比赛
        startTheGame = new LadderPanelBtn("inkImg/button/ttgo.png", 1, 1);
        startTheGame.setBounds(550, 61, 100, 19);
        this.add(startTheGame);
        //天梯积分
        ladderPoints = new LadderPanelBtn("inkImg/button/ttjf.png", 1, 2);
        ladderPoints.setBounds(550, 91, 100, 19);
        this.add(ladderPoints);
        //天梯赛季奖励
        ladderLottery = new LadderPanelBtn("inkImg/button/ttjc.png", 1, 3);
        ladderLottery.setBounds(550, 121, 100, 19);
        this.add(ladderLottery);
        //奖励1
        award1 = new LadderPanelBtn("inkImg/button/ttcc1.png", 1, 4);
        award1.setBounds(450, 60, 100, 25);
        this.add(award1);
        //奖励2
        award2 = new LadderPanelBtn("inkImg/button/ttcc10.png", 1, 5);
        award2.setBounds(450, 90, 100, 25);
        this.add(award2);
        //奖励3
        award3 = new LadderPanelBtn("inkImg/button/ttcc20.png", 1, 6);
        award3.setBounds(450, 120, 100, 25);
        this.add(award3);



        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 604);
        offBtn.setBounds(684 - 37, 10, 25, 25);
        this.add(offBtn);
    }

    @Override
    protected void paintComponent(Graphics g) {

        ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
        Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
        Configure configure = item.get(new BigDecimal(1));
        String nao = "新";
        if(configure.getNeworold()!=null) {
            nao = configure.getNeworold();
        }

        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(beijing.getImage(), 0, 0, 674, 449, null);
        LoginResult login = RoleData.getRoleData().getLoginResult();
        BigDecimal ttRecord = login.getScoretype("天梯积分");
        String userName = login.getRolename();
        g.setFont(UIUtils.TEXT_BOLD_FONT);

        if (loginResultList != null) {
            int x = loginResultList.size();
            if(x>5) {
                x = 5;
            }
            g.setFont(UIUtils.TEXT_FONT);
            for (int i = 0; i < x; i++) {

                LoginResult loginResult = loginResultList.get(i);
//                int cout = loginResult.getTtFail() + loginResult.getFighting();
//                if (cout == 0)
//                    continue;teamArena
                g.setColor(Color.black);

                String skin = loginResult.getSpecies_id().toString();
                if(!nao.equals("新")) {
                    Object part = SpriteFactory.createPart(skin, 2, 1, null);
                    if (part instanceof PartTwo) {
                        ((PartTwo) part).draw(g, 100 + i * 120, 270, 4, ImageMixDeal.userimg.getTime());
                    } else if (part instanceof NewPart) {
                        ((NewPart) part).draw(g, 100 + i * 120, 270, 4, ImageMixDeal.userimg.getTime());
                    }
                }else {
                    Object part = SpriteFactory.createPart(skin, 2, 1, null);
                    if (part instanceof PartTwo) {
                        ((PartTwo) part).draw(g, 100 + i * 120, 270, 4, ImageMixDeal.userimg.getTime());
                    } else if (part instanceof NewPart) {
                        ((NewPart) part).draw(g, 100 + i * 120, 270, 4, ImageMixDeal.userimg.getTime());
                    }
                }
                Ladder ladder = getLadder(loginResult.getGrade());
                g.setColor(ladder.getColor());
                g.drawString(loginResult.getRolename(), 95 + i * 115, 325);
                g.drawString(ladder.getLevel(), 95 + i * 115, 342);
                g.setColor(Color.black);
                int victory = loginResult.getTtVictory() == null ? 0 : loginResult.getTtVictory();
                int fail = loginResult.getTtFail() == null ? 0 : loginResult.getTtFail();
                g.drawString("胜" + victory + "/败" + fail, 95 + i * 115, 380);
//                g.drawString("胜" + loginResult.getTtVictory() + "/败" + loginResult.getTtFail(), 95 + i * 115, 380);
//                g.drawString(loginResult.getScoretype("天梯积分").toString(), 95 + i * 115, 360);
                g.drawString(loginResult.getTtRecord() == null ? "0": loginResult.getTtRecord().toString(), 95 + i * 115, 360);
            }
        }

        Boolean b = false;
        Integer index = 0;
        if (loginResultList != null) {
            for (LoginResult loginResult : loginResultList) {
                index += 1;
                if (loginResult.getRole_id().compareTo(login.getRole_id()) == 0) {
                    b = true;
                    break;
                }
            }
        }

        Ladder ladder = getLadder(login.getGrade());

//        g.setColor(ladder.getColor());
        g.setColor(Color.black);
        g.drawString(userName, 150, 75);
        g.drawString(ladder.getLevel(), 150, 105);
        g.drawString(ttRecord.intValue() + "", 150, 135);
        g.drawString("胜" + login.getTtVictory() + "/败" + login.getTtFail(), 350, 75);
        g.drawString(b ? "第 " + index + " 名" : "暂未上榜", 350, 105);
        g.drawString("暂无排名", 350, 136);


        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setFont(UIUtils.TEXT_HYK20);
        if(this.currSeason!=null) {
            g2d.drawString(this.currSeason, 50, 40);
            g2d.setFont(UIUtils.TEXT_NAME_FONT);
            g2d.setColor(Color.red);
            g2d.drawString(this.seasonInfo, 50, 170);
        }

    }

    private Ladder getLadder(Integer grade) {
        Ladder ladder = new Ladder();
        // 判断人物的等级
        if (grade <= 102) {
            ladder.setColor(new Color(0, 255, 0));
            ladder.setLevel("0转" + grade + "级");
        } else if (grade > 102 && grade <= 210) {
            ladder.setColor(new Color(255, 140, 0));
            ladder.setLevel("1转" + (grade - 102 + 14) + "级");
        } else if (grade > 210 && grade <= 338) {
            ladder.setColor(new Color(0, 245, 255));
            ladder.setLevel("2转" + (grade - 210 + 14) + "级");
        } else if (grade > 338 && grade <= 459) {
            ladder.setColor(new Color(238, 44, 44));
            ladder.setLevel("3转" + (grade - 338 + 59) + "级");
        } else if (grade > 459) {
            ladder.setColor(new Color(155, 48, 255));
            ladder.setLevel("飞升" + (grade - 459 + 139) + "级");
        }
        return ladder;
    }
}


